<template>
	<view class="merch">
		<Header v-model="keywords" @change="handleChange"></Header>
		<view class="list" v-if="list.length">
			<view class="list-item" v-for="(item, index) in list" :key="index" @click="toMerchDetail(item.id)">
				<u--image width="340rpx" height="300rpx" radius="20rpx 20rpx 0rpx 0rpx" :src="item.firstMedia"
					mode="aspectFill"></u--image>
				<view class="info">
					<view class="title">{{item.name}}</view>
					<view class="subtitle">{{item.merchTypeName}}</view>
					<view class="subtitle">{{item.producer}}出品</view>
				</view>
			</view>
			<u-loadmore :status="loadmoreStatus" :marginTop="30" :marginBottom="30" line />
		</view>
		<view class="empty" v-else>
			<u-empty mode="search" :iconSize="120" text="周边为空"></u-empty>
		</view>
		<LoginModal ref="loginModal" />
	</view>
</template>

<script>
	import Header from '@/components/header.vue';
	import {
		checkLogin
	} from '@/utils/index.js';

	export default {
		components: {
			Header,
		},
		data() {
			return {
				keywords: '',
				page: 1,
				size: 10,
				list: [],
				loadmoreStatus: 'loadmore',
			}
		},
		onLoad() {
			this.getList();
		},
		onPullDownRefresh() {
			this.page = 1;
			this.list = [];
			this.getList();
			uni.stopPullDownRefresh();
		},
		onReachBottom() {
			if(this.loadmoreStatus === 'nomore') return;
			this.loadmoreStatus = 'loading';
			this.page += 1;
			this.getList();
		},
		methods: {
			async getList() {
				const res = await this.$api('/study/mobile/merch/page', 'get', {
					page: this.page,
					limit: this.size,
					keywords: this.keywords,
				});
				
				this.loadmoreStatus = 'loadmore';
				this.list = this.list.concat(res.data.list);
				
				if (res.data.list.length < this.size) {
					this.loadmoreStatus = 'nomore';
					return;
				}
			},

			handleChange(e) {
				this.keywords = e;
				this.page = 1;
				this.list = [];
				this.loadmoreStatus = 'loadmore';
				this.getList();
			},

			toMerchDetail(id) {
				if(checkLogin()) {
					uni.navigateTo({
						url: `/pages/study/merch-detail?id=${id}`
					})
				} else {
					this.$refs.loginModal.open();
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.merch {
		padding: 0 30rpx 60rpx;
	}

	.list {
		display: flex;
		flex-direction: row;
		flex-wrap: wrap;
		justify-content: space-between;

		.list-item {
			width: 340rpx;
			background-color: #fff;
			margin-top: 20rpx;
			border-radius: 20rpx;

			.info {
				padding: 30rpx 20rpx;

				.title,
				.subtitle {
					overflow: hidden;
					white-space: nowrap;
					text-overflow: ellipsis;
				}

				.title {
					font-size: 28rpx;
				}

				.subtitle {
					font-size: 26rpx;
				}
			}
		}
	}
</style>